﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;

namespace PX.Coureseware
{
	/// <summary>
	/// 视图控制类
	/// </summary>
	/// <![CDATA[2014-8-7 17:52:59]]>
	public class View : ShowLayerV2.FormView
	{
		private Component clsthisComp;

		#region 添加、修改
		private TextBox tbDate = new TextBox();
		private Brettle.Web.NeatUpload.InputFile inputFile = new Brettle.Web.NeatUpload.InputFile();
		#endregion 添加、修改

		#region 查询
		private TextBox tbQueryStartDate = new TextBox();
		private TextBox tbQueryEndDate = new TextBox();
		private TextBox tbQueryName = new TextBox();
		#endregion 查询

		#region 浏览

		#endregion 浏览

		#region 构造函数
		/// <summary>
		/// 视图类构造函数
		/// </summary>
		/// <![CDATA[2014-9-16 9:28:28]]>
		public View(System.Web.UI.Page page)
			: base(page)
		{
			clsthisComp = new Component(page);

			clsComp = clsthisComp;

			tbDate.Attributes.Add("onClick", "popUpCalendar(this, this, 'yyyy-mm-dd')");
			tbQueryStartDate.Attributes.Add("onClick", "popUpCalendar(this, this, 'yyyy-mm-dd')");
			tbQueryEndDate.Attributes.Add("onClick", "popUpCalendar(this, this, 'yyyy-mm-dd')");

			listQuery.Add(new TonyNet4V1.Structs.TitleControlEntity("开始日期", "TextBox", tbQueryStartDate, "string", "strStartDate"));
			listQuery.Add(new TonyNet4V1.Structs.TitleControlEntity("结束日期", "TextBox", tbQueryEndDate, "string", "strEndDate"));
			listQuery.Add(new TonyNet4V1.Structs.TitleControlEntity("名称", "TextBox", tbQueryName, "string", "strName"));
		}
		#endregion 构造函数

		#region 类型转换

		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}

		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}

		protected override TonyNet4V1.ComponentBase GetComponent()
		{
			return clsthisComp;
		}

		#endregion 类型转换

		public override void AddEditLayout(Panel pPanel)
		{
			//base.AddEditLayout(pPanel);

			Table tabAddEdit = new Table();
			TableRow trOne = new TableRow();
			TableRow trTwo = new TableRow();
			TableCell tc11 = new TableCell();
			TableCell tc12 = new TableCell();
			TableCell tc21 = new TableCell();
			TableCell tc22 = new TableCell();

			tabAddEdit.CssClass = table.CssClass;
			trOne.CssClass = tableRow.CssClass;
			trTwo.CssClass = tableRow.CssClass;
			tc11.CssClass = tableCellOne.CssClass;
			tc12.CssClass = tableCellTwo.CssClass;
			tc21.CssClass = tableCellOne.CssClass;
			tc22.CssClass = tableCellTwo.CssClass;

			tbDate.Text = System.DateTime.Now.ToShortDateString();

			tc11.Text = "日期";
			tc12.Controls.Add(tbDate);
			trOne.Controls.Add(tc11);
			trOne.Controls.Add(tc12);
			tabAddEdit.Controls.Add(trOne);

			tc21.Text = "课件";
			tc22.Controls.Add(inputFile);
			trTwo.Controls.Add(tc21);
			trTwo.Controls.Add(tc22);
			tabAddEdit.Controls.Add(trTwo);

			pPanel.Controls.Add(tabAddEdit);
		}

		public override bool Add()
		{
			//return base.Add();

			FileInfo fi = new FileInfo(inputFile.FileName);

			//if (fi.Extension.ToLower() == ".mp4" || fi.Extension.ToLower() == ".flv")
			//{
				string folder = System.Web.HttpContext.Current.Server.MapPath("~/UploadFiles/" + TonyNet4V1.CommonFunction.GetNowYearMonth() + "/");
				//string saveFileName = string.Format("{0}{1:yyMMdd-HHmmss}-{2,3}{3}",
				//    folder,
				//    DateTime.Now,
				//    new Random().Next(0, 999),
				//    fi.Extension
				//    );

				string strConvertFileName = TonyNet4V1.CommonFunction.GetNowDateTime() + fi.Extension;

				string saveFileName = folder + strConvertFileName;

				if (!Directory.Exists(folder))
					Directory.CreateDirectory(folder);
				if (inputFile.ContentLength > 0)
					inputFile.MoveTo(saveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);

				Entity clsCourseware = new Entity();

				clsCourseware.Name = inputFile.FileName;
				clsCourseware.Date = tbDate.Text;
				clsCourseware.DateTime = System.DateTime.Now.ToString();
				clsCourseware.UserID = nUserID;
				clsCourseware.DepartmentID = nDepartmentID;
				clsCourseware.Path = "/UploadFiles/" + TonyNet4V1.CommonFunction.GetNowYearMonth() + "/" + strConvertFileName;

				return clsthisComp.Add(clsCourseware);
			//}
			//else
			//{
			//	TonyNet4V1.ShowMessageAlert.ShowMessage(page, "文件上传格式错！只能上传mp4和flv格式文件");
			//	return false;
			//}
		}

		public override bool QueryDefault(GridView gv)
		{
			if (bQueryByUserID)
			{
				gv.DataSource = clsthisComp.QueryByUserID(nUserID);
				gv.DataKeyNames = new string[] { "ID" };
				gv.DataBind();
				return true;
			}
			else
			{
				return base.QueryDefault(gv);
			}
		}

		public override bool Query(GridView gv)
		{
			if (bQueryByUserID)
			{
				QueryEntity clsQuery = new QueryEntity();

				ControlToQuery(clsQuery, listQuery);
				clsQuery.nUserID = nUserID;

				gv.DataSource = clsthisComp.Query(clsQuery);
				gv.DataKeyNames = new string[] { "ID" };
				gv.DataBind();
				return true;
			}
			else
			{
				return base.Query(gv);
			}
		}
	}
}
